package com.yanqu.road.server.cmd.activity.seacraftsouthsea;

import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.SeacraftSouthSeaProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.SeacraftSouthSeaCommonMgr;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftSouthSeaMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;

/**
 * @Description
 * @Author cwq
 * @Data 2022/1/20 15:28
 */
@Cmd(code = GameProtocol.S_SEACRAFT_SOUTH_SEA_ACTION, desc = "海上争霸 南海丝路 进攻 / 突袭 / 使用道具进攻 / 修复")
public class SeacraftSouthSeaActionCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SeacraftSouthSeaProto.SeacraftSouthSeaActionReqMsg reqMsg = SeacraftSouthSeaProto.SeacraftSouthSeaActionReqMsg.parseFrom(packet.getBytes());
        ret = SeacraftSouthSeaMgr.canEntryActivity(player,reqMsg.getActivityId());
        if(ret != 0){
            return;
        }
        // 判断是否在活动期
        if(!SeacraftSouthSeaCommonMgr.isOpenTime(SeacraftSouthSeaMgr.getActivityInfo(),SeacraftSouthSeaMgr.getConfig())){
            ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_NO_OPEN;
            return;
        }

        if(!SeacraftSouthSeaMgr.isPromotion(player.getUserInfo().getUnionUid())){
            ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_TARGET_NO_PROMOTION;
            return;
        }

        // 操作类型 0 进攻 1突袭 2使用道具进攻 3修复 4 增加小队进攻次数 5 使用船桨
        SeacraftSouthSeaProto.SeacraftSouthSeaActionGameToCrossMsg.Builder gameToCrossMsg = SeacraftSouthSeaProto.SeacraftSouthSeaActionGameToCrossMsg.newBuilder();
        gameToCrossMsg.setActivityId(reqMsg.getActivityId());
        gameToCrossMsg.setCtsMsg(reqMsg);
        gameToCrossMsg.setUnionId(player.getUserInfo().getUnionUid());
        gameToCrossMsg.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        if(reqMsg.getType() == 0){
            // 攻击
            long count = player.getModule(BagModule.class).getUserBagItemCount(SeacraftSouthSeaMgr.getConfig().getSEACRAFT_CRAFT_ATTACK_ITEM_ID());
            if(count <= 0){
                ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                return;
            }
            Property property = new Property(SeacraftSouthSeaMgr.getConfig().getSEACRAFT_CRAFT_ATTACK_ITEM_ID(), count);
            // 预扣除
            player.getModule(PreReduceModule.class).preReduce(property);
            gameToCrossMsg.setPreReduceCount(count);
            gameToCrossMsg.setPropId(SeacraftSouthSeaMgr.getConfig().getSEACRAFT_CRAFT_ATTACK_ITEM_ID());
        }else if(reqMsg.getType() == 2 || reqMsg.getType() == 3 || reqMsg.getType() == 5) {
            gameToCrossMsg.setTitleId(player.getUserInfo().getTitleId());
            if(reqMsg.getCount() <= 0){
                ret = GameErrorCode.E_BAG_USE_GOODS_COUNT_LESS_ONE;
                return;
            }
            int propId = 0;
            if(reqMsg.getType() == 2 ){
                propId = SeacraftSouthSeaMgr.getConfig().getSEACRAFT_CRAFT_FIRE_ITEM_ID();
            }else if(reqMsg.getType() == 3){
                propId = SeacraftSouthSeaMgr.getConfig().getSEACRAFT_CRAFT_WRENCH_ITEM_ID();
            }else if(reqMsg.getType() == 5){
                if(!SeacraftSouthSeaMgr.getConfig().hasPaddleId(reqMsg.getPropId())){
                    ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_PADDLE_ID_ERROR;
                    return;
                }
                propId = reqMsg.getPropId();
            }
            Property property = new Property(propId, reqMsg.getCount());
            if(!player.getModule(CurrencyModule.class).currencyIsEnough(property)){
                ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                return;
            }
            // 预扣除
            player.getModule(PreReduceModule.class).preReduce(property);
            gameToCrossMsg.setPreReduceCount(reqMsg.getCount());
            gameToCrossMsg.setPropId(propId);
        }
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_SEACRAFT_SOUTH_SEA_ACTION, gameToCrossMsg, reqMsg.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            SeacraftSouthSeaProto.SeacraftSouthSeaActionRespMsg.Builder resp = SeacraftSouthSeaProto.SeacraftSouthSeaActionRespMsg.newBuilder();
            resp.setRet(ret);
            player.sendPacket(ClientProtocol.U_SEACRAFT_SOUTH_SEA_ACTION,resp);
        }
    }
}
